<?
//	require_once("mysql.html");

//===============================================
// this class will create the Hotels Vouchers    
//===============================================
class Voucher{

	var $HotelName, $HotelAddress, $HotelPhone, $HotelFax;
	var $pdf;
	var $VoucherNum;
	var $AdminInfo;
	var $Passenger, $RoomType, $Breakfast, $CheckInOut, $Nights, $ConfNum;
	var $admin = false;
	var $FromMagicName = "";
	var $FromMagicCompany = "FIT Department";
        var $file_name_prefix = "";
        var $PaymentBy = "";
function Voucher($admin_,$headerID="")
{
	global $db;
//	error_reporting(E_ALL);
	set_time_limit(1800);

	include 'class.ezpdf.php';
	$this->pdf = new Cezpdf('a4','portrait');

	$this->admin = $admin_;

//	require_once((($this->admin)?"../":"")."includes/mysql.html");

	$head = "";
	if( ($headerID=="")||(!is_numeric($headerID)) )
	{
		  $head = $db->select("SELECT * FROM images WHERE imgDefault = 1");
    	          $head = $head[0];
		  if(!$head) 
		  {
			  // echo "GetDefault();";
			  $head = $db->select("SELECT * FROM images");
	    	          $head = $head[0];
		  }
	}else
	{
		$head = $db->select("SELECT * FROM images WHERE imgID = '".$headerID."'");
                $head = $head[0];
  	}
	$this->SetOwnerInfoLogo($head);
}

// Set Owner Informations with LogoFile
function SetOwnerInfoLogo($head){
	$this->logo_file = (($this->admin)?"../":"")."images/pdf_logo/".$head["imgName"];
	$this->ownerAddress	= $head["imgInfo1"];
	$this->ownerCity	  = $head["imgInfo2"];
	$this->ownerPhone	  = $head["imgInfo3"];
	$this->ownerFax		  = $head["imgInfo4"];
	$this->ownerURL		  = $head["imgInfo5"];
	$this->ownerEmail	  = $head["imgInfo6"];
	$this->PaymentBy	  = $head["imgInfo7"];
}

function SetPaymentBy($payment_by)
{
  $this->PaymentBy = $payment_by;
}

// Set Hotel Informations
// $HotelName, $HotelAddress1, $HotelAddress2, $HotelPhone, $HotelFax
function SetHotelInfo($Name, $Address1, $Address2="", $Phone, $Fax, $ReservationFax=""){
	$this->HotelName	= wordwrap($Name, 40, "\n", 1);
	$this->HotelAddress	= wordwrap($Address1." ".$Address2, 35, "\n", 1);
	$this->HotelPhone	= $Phone;
	$this->HotelFax		= $Fax;
	$this->HotelReservationFax		= $ReservationFax;
}

// Set Ref Info
function SetRefInfo($Num, $Company=""){
	$this->ClientRef = $Num;
	$this->ClientCompany = $Company;
}

// Set Voucher Number
function SetVoucherNum($Num){
	$this->VoucherNum = $Num;
}

// Set Admin information (for reports vauchers to other hotels)
function SetAdminInfo($info){
	$this->AdminInfo = wordwrap($info, 90, "\n", 1);
}

//
// $Passenger - $Name of agent
// $RoomType: (see table 'roomtypes')
//		Single, Suite - Park View, Single - Standard, Double - Standard, Quad ... etc 
// $Breakfast - if there is no breakfast policy in the hotel info than it's "ROOM ONLY"
// $CheckIn, $CheckOut Day (2005-06-05)
// $ConfNum - hotel confirmation number if there is one (may be empty)
//
function SetOrderInfo($Passenger, $RoomType, $Breakfast="", $CheckIn, $CheckOut, $Nights, $ConfNum){
	$this->Passenger	= $Passenger;
	$this->RoomType		= $RoomType;
	$this->Breakfast	= ($Breakfast=="")?"ROOM ONLY":$Breakfast;

	$my_Date = "";
	$my_Hour = "";
	$my_Minute = "";
	if( ($CheckIn)&&($CheckOut) ){
		$in_Year  = substr($CheckIn,0,4);
		$in_Day   = substr($CheckIn,8,2);
		$in_Month = substr($CheckIn,5,2);

		$out_Year  = substr($CheckOut,0,4);
		$out_Day   = substr($CheckOut,8,2);
		$out_Month = substr($CheckOut,5,2);

		$this->CheckInOut = ($in_Year==$out_Year)
			? $in_Month."/".$in_Day."-".$out_Month."/".$out_Day."/".$out_Year
			: $in_Month."/".$in_Day."/".$in_Year."-".$out_Month."/".$out_Day."/".$out_Year;
	}
	else
		$this->CheckInOut	= "";

	$this->Nights		= $Nights;
	$this->ConfNum		= $ConfNum;
}

function _Generate_Header(){
	$this->pdf->ezSetMargins(50,70,50,50);

	//$mainFont = './fonts/Helvetica.afm';
	//$codeFont = './fonts/Courier.afm';
	// select a font
	$mainFont = (($this->admin)?"../":"").'includes/fonts/Times-Roman.afm';
	$this->pdf->selectFont($mainFont);

	// put a line top and bottom on all the pages

	$all = $this->pdf->openObject();
	$this->pdf->saveState();
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->line(30,40,550,40); //$this->pdf->line(20,822,578,822);
	$this->pdf->addText(50,34,6,'http://magictours.us - Magic Tours & Travel LLC');
	$this->pdf->restoreState();
	$this->pdf->closeObject();

	// note that object can be told to appear on just odd or even pages by changing 'all' to 'odd' or 'even'.
	$this->pdf->addObject($all,'all');

	$koef = 1;
	$h = 100;
	//echo $this->logo_file;
	if (file_exists($this->logo_file)){
		$size=getimagesize($this->logo_file);
		$w=$size[0];
		$h=$size[1];

		$koef = 200/$w;
		$this->pdf->addJpegFromFile($this->logo_file, 90, $this->pdf->y-$h*$koef, 200, 0);
	} 
	$this->pdf->rectangle(90, $this->pdf->y-$h*$koef, 350, $h*$koef);

	// BLOCK 0
	$data0 = array(
		 array('key'=>'<i><b>'.$this->ownerAddress.'</b></i>',	'value'=>'')
		,array('key'=>'<i><b>'.$this->ownerCity.'</b></i>',		'value'=>'')
		,array('key'=>'<i><b>'.$this->ownerPhone.'</b></i>',	'value'=>'')
		,array('key'=>'<i><b>'.$this->ownerFax.'</b></i>',		'value'=>'')
		,array('key'=>'<i><b>'.$this->ownerURL.'</b></i>',		'value'=>'')
		,array('key'=>'<i><b>'.$this->ownerEmail.'</b></i>',	'value'=>'')
		);
	$this->pdf->ezTable($data0,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>10, 'xPos'=>'center', 'xOrientation'=>'right' )
			);
	//echo 90-$h*$koef;
	$this->pdf->ezSetDy(-$h*$koef+90);

}

function _Create_Body_V234(){
	$this->pdf->ezSetDy(-90);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->line(30, $this->pdf->y, 550, $this->pdf->y);
	$this->pdf->ezSetDy(-2);
	$this->pdf->line(30, $this->pdf->y, 550, $this->pdf->y);
	$this->pdf->ezSetDy(-4);
	$data4 = array(
		 array('key'=>'    OUR REF :',			'value'=>'<b>'.$this->VoucherNum.'</b>')
		,array('key'=>'    AGENT REF :',		'value'=>'<b>'.$this->ClientRef.'</b>')
		,array('key'=>'    Number of nights :',	'value'=>'<b>'.$this->Nights.' night'.(($this->Nights!="1")?'s':'').'</b>')
		,array('key'=>'    CONF :',				'value'=>'<b>'.$this->ConfNum.'</b>')
		);
	$this->pdf->ezTable(
			$data4, 
			array('key'=>'<i>Key</i>', 'value'=>'Value'),
			'',
			array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>12, 'xPos'=>'center', 'xOrientation'=>'right' )
		);

	$this->pdf->ezSetDy(69);
	$data1a = array(
		 array('key'=>'',					'value'=>'<b>(SURNAME, FIRST/TITLE)</b>')
		,array('key'=>"Client's Name :",	'value'=>'<b>'.$this->Passenger.'</b>')
		,array('key'=>"Check in/out :",		'value'=>'<b>'.$this->CheckInOut.'</b>')
		,array('key'=>"Type of room :",		'value'=>'<b>'.$this->RoomType.'</b>')
		);
	$this->pdf->ezTable($data1a,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>12, 'xPos'=>'left', 'xOrientation'=>'right' )
			);
	$this->pdf->ezSetDy(-15);
	$this->pdf->ezText("<b>".$this->AdminInfo."</b>", 12 ,array('justification'=>'left'));

	$this->pdf->ezSetDy(-10);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->line(30, $this->pdf->y, 550, $this->pdf->y);
	$this->pdf->ezSetDy(-1);
	$this->pdf->line(30, $this->pdf->y, 550, $this->pdf->y);
	$this->pdf->ezSetDy(-10);
}

// Create Voucher N1
function Create_V1(){
	$this->_Generate_Header();
	// BLOCK 1 -- Hotel Info
	$this->pdf->ezSetDy(-20);

	// BLOCK 1a
	$data1a = array(
	     array('key'=>'                 ',			'value'=>date("l, M j Y") )
	    ,array('key'=>'                 Client ref#:','value'=>$this->ClientRef)
	    ,array('key'=>'                 Company :',	'value'=>$this->ClientCompany)
	    );
	$this->pdf->ezTable($data1a,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>11, 'xPos'=>'center', 'xOrientation'=>'right' )
			);

	$this->pdf->ezSetDy(50);
	$data1 = array(
	     array('key'=>'To :',		'value'=>'<b>'.$this->HotelName.'</b>')
	    ,array('key'=>'Address :',	'value'=>$this->HotelAddress)
	    ,array('key'=>'Phone :',	'value'=>$this->HotelPhone)
	    //,array('key'=>'FAX :',		'value'=>$this->HotelFax)
	    );
	$this->pdf->ezTable($data1,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>12, 'xPos'=>'left', 'xOrientation'=>'right' )
			);

	// BLOCK 2
	$this->pdf->ezSetDy(-5);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->rectangle(130, $this->pdf->y-14, 280, -20);
	$this->pdf->ezText("<b>ORIGINAL</b>", 14 ,array('justification'=>'left'));

	$this->pdf->ezSetDy(5);
	$data2 = array(
	     array('key'=>'<b><u>VOUCHER No. </u></b>',	'value'=>'<u><b>'.$this->VoucherNum.'</b></u>')
	    );
	$this->pdf->ezTable($data2,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0, 'shaded'=>0, 'showLines'=>0, 'fontSize'=>14, 'xPos'=>'center' )
			);

	// BLOCK 3
	$this->pdf->ezSetDy(-20);

	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->line(40, $this->pdf->y, 540, $this->pdf->y);
	$this->pdf->ezSetDy(-2);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->rectangle(40, $this->pdf->y, 500, -220);
	$data3_a = array(
	     array('key'=>'Please provide for the following passengers:           ',		'value'=>'<b>'.$this->Passenger.'</b>')
	    );
	$this->pdf->ezTable($data3_a,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0, 'shaded'=>0, 'showLines'=>0, 'fontSize'=>12, 'xPos'=>'left', 'xOrientation'=>'right' )
			);

	$this->pdf->ezSetDy(-5);
	$this->pdf->ezText("<b>Service as follows :</b>", 12 ,array('justification'=>'left'));

	// BLOCK 4
	$this->pdf->ezSetDy(-4);
	$data4 = array(
		array('key'=>'Type of room :',		'value'=>'<b>'.$this->RoomType.'</b>')
		,array('key'=>'On Basis :',			'value'=>'<b>'.$this->Breakfast.'</b>')
		,array('key'=>'Check in/out :',		'value'=>'<b>'.$this->CheckInOut.'</b>')
		,array('key'=>'Total of Nights :',	'value'=>'<b>'.$this->Nights.' night'.(($this->Nights!="1")?'s':'').'</b>')
		,array('key'=>'CONFIRMATION No :',	'value'=>'<b>'.$this->ConfNum.'</b>')
		);
	$this->pdf->ezTable(
			$data4, 
			array('key'=>'<i>Key</i>', 'value'=>'Value'),
			'',
			array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>12, 'xPos'=>'left', 'xOrientation'=>'right' )
		);
	$this->pdf->ezSetDy(-50);

	// BLOCK 5
	$this->pdf->ezSetDy(-100);
	$this->pdf->ezText("<b>Payment by ".$this->PaymentBy."</b>", 11 ,array('justification'=>'left'));
	
	$this->pdf->ezSetDy(17);
	$data2 = array(
	     array( 'value'=>'', 
				'x1'=>'X', 'Hotel'=>'Room', 
				'x2'=>'X', 'Tax'=>'Tax')
	    );
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->rectangle(338,  $this->pdf->y-5, 15, -15);
	$this->pdf->rectangle(408,  $this->pdf->y-5, 15, -15);
	$this->pdf->ezTable($data2,
				array('value'=>'Value', 
						'x1'=>'x1', 'Hotel'=>'Hotel ',
						'x2'=>'x2', 'Tax'=>'Tax'),
				'',
				array('showHeadings'=>0, 'shaded'=>0, 'showLines'=>0, 'fontSize'=>14, 'xPos'=>'center', 'xOrientation'=>'right' )
			);

	// BLOCK 6
	$this->pdf->ezSetDy(-15);
	$this->pdf->ezText("This voucher covers payments for the services mentioned above.", 11 ,array('justification'=>'left'));
	
	$this->pdf->ezSetDy(-10);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->rectangle(45,  $this->pdf->y, 270, -18);
	$this->pdf->ezText("<u>Any extra items must be charged directly to the clients.</u>", 11 ,array('justification'=>'left'));
}


// Create Voucher N2
function Create_V2(){
	$this->_Generate_Header();

	// BLOCK 1 -- Hotel Info
	$this->pdf->ezSetDy(-20);
	$this->pdf->ezText("<b>".$this->HotelName."</b>", 13 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	$this->pdf->ezText("Attn:   ".$this->FromMagicCompany, 11 ,array('justification'=>'left'));
	$this->pdf->ezText("From: ".$this->FromMagicName, 11 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->ezText("<u><b>Please CONFIRM the following AMENDMENT:</b></u>", 12 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	//$this->pdf->ezText("<b>FAX:  </b>".$this->HotelFax."              <b>TEL:  </b>".$this->HotelPhone."              <b>Reservation FAX:  </b>".$this->HotelReservationFax."              <b>", 11 ,array('justification'=>'left'));
	$this->pdf->ezText("<b>TEL:  </b>".$this->HotelPhone."              <b>Reservation FAX:  </b>".$this->HotelReservationFax."              <b>", 11 ,array('justification'=>'left'));

	$this->pdf->ezSetDy(80);
	$data1a = array(
	     array('key'=>'                 ',			'value'=>date("l, M j Y") )
	    );
	$this->pdf->ezTable($data1a,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>11, 'xPos'=>'center', 'xOrientation'=>'right' )
			);

	$this->_Create_Body_V234();

	$this->pdf->ezText("<b>Best regards, \n\n".$this->FromMagicName.$this->FromMagicCompany."</b>", 11 ,array('justification'=>'left'));
}

// Create Voucher N3
function Create_V3(){
	$this->_Generate_Header();

	// BLOCK 1 -- Hotel Info
	$this->pdf->ezSetDy(-20);
	$this->pdf->ezText("<b>".$this->HotelName."</b>", 13 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	$this->pdf->ezText("Attn:   ".$this->FromMagicCompany, 11 ,array('justification'=>'left'));
	$this->pdf->ezText("From: ".$this->FromMagicName, 11 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	//$this->pdf->ezText("<b>FAX:  </b>".$this->HotelFax."              <b>TEL:  </b>".$this->HotelPhone."              <b>Reservation FAX:  </b>".$this->HotelReservationFax."              <b>", 11 ,array('justification'=>'left'));
	$this->pdf->ezText("<b>TEL:  </b>".$this->HotelPhone."              <b>Reservation FAX:  </b>".$this->HotelReservationFax."              <b>", 11 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->ezText("<u><b>Please CANCEL BY FAX the following:</b></u>", 12 ,array('justification'=>'left'));

	$this->pdf->ezSetDy(80);
	$data1a = array(
	     array('key'=>'                 ',			'value'=>date("l, M j Y") )
	    );
	$this->pdf->ezTable($data1a,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>11, 'xPos'=>'center', 'xOrientation'=>'right' )
			);

	$this->_Create_Body_V234();

	$this->pdf->ezText("<b>Sorry, ".$this->FromMagicName.", ".$this->FromMagicCompany."</b>", 11 ,array('justification'=>'left'));
}

// Create Voucher N4
function Create_V4(){
	$this->_Generate_Header();

	// BLOCK 1 -- Hotel Info
	$this->pdf->ezSetDy(-20);
	$this->pdf->ezText("<b>".$this->HotelName."</b>", 13 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	$this->pdf->ezText("Attn:   ".$this->FromMagicCompany, 11 ,array('justification'=>'left'));
	$this->pdf->ezText("From: ".$this->FromMagicName, 11 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	$this->pdf->setStrokeColor(0,0,0,1);
	$this->pdf->ezText("<u><b>Please BOOK and CONFIRM FIT rate for the following:</b></u>", 12 ,array('justification'=>'left'));
	$this->pdf->ezSetDy(-5);
	//$this->pdf->ezText("<b>FAX:  </b>".$this->HotelFax."              <b>TEL:  </b>".$this->HotelPhone."              <b>Reservation FAX:  </b>".$this->HotelReservationFax."              <b>", 11 ,array('justification'=>'left'));
	$this->pdf->ezText("<b>TEL:  </b>".$this->HotelPhone."              <b>Reservation FAX:  </b>".$this->HotelReservationFax."              <b>", 11 ,array('justification'=>'left'));

	$this->pdf->ezSetDy(80);
	$data1a = array(
	     array('key'=>'                 ',			'value'=>date("l, M j Y") )
	    );
	$this->pdf->ezTable($data1a,
				array('key'=>'<i>Key</i>', 'value'=>'Value'),
				'',
				array('showHeadings'=>0,'shaded'=>0,'showLines'=>0, 'fontSize'=>11, 'xPos'=>'center', 'xOrientation'=>'right' )
			);

	$this->_Create_Body_V234();

	$this->pdf->ezText("<b>Best regards, \n\n".$this->FromMagicName.$this->FromMagicCompany."</b>", 11 ,array('justification'=>'left'));
}

function return_pdf(){  
	return $this->pdf->getPdfStream();

}

function Show(){  
	$this->pdf->ezStream();
}

function Save(){  
//	error_reporting(0);
	$filename = $this->file_name_prefix.str_replace(" ","",$this->VoucherNum).".pdf";
	header("Content-type: application/pdf");
	header("Content-Disposition: attachment; filename=$filename" );
	echo $this->pdf->getPdfStream();
}

}
// End of CLASS Voucher()

?>
